package by.exposit.dao;

import by.exposit.dao.entity.*;
import by.exposit.domain.*;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HaRiA on 30.07.2014.
 */
@Repository
@Transactional
public class RequestDAOImpl extends GenericDAOImpl<RequestEntity,Long> implements RequestDAO {


    @Override
    public List<RequestEntity> requestUserList(UserEntity userEntity) {
        List<RequestEntity> requests = null;
        Criteria cr = getSession()
                .createCriteria(RequestEntity.class, "Request")
                .add(Restrictions.eq("user", userEntity));
        return cr.list();
    }



    @Override
    public List<RequestEntity> requestFieldList(FieldEntity fieldEntity) {

        Criteria cr = getSession().createCriteria(ProjectEntity.class, "Project")
                .add(Restrictions.eq("field",fieldEntity));
           List<ProjectEntity> projectEntities = cr.list();

        List<RequestEntity> requestEntities = new ArrayList<RequestEntity>();

        for(ProjectEntity projectEntity: projectEntities) {
            cr = getSession()
                    .createCriteria(RequestEntity.class, "Request")
                    .add(Restrictions.eq("project", projectEntity));
            requestEntities.add((RequestEntity)cr.uniqueResult());
        }
        return requestEntities;
    }




    public void changeStatusRequest(Request request,Status status){

    }

    @Override
    public  void  allocateMoney(RequestEntity requestEntity)
    {



    }




    public List<RequestEntity> satisfyRequest(){
        return null;
    }

    public List<RequestEntity> financedButNotExecuted(){
        return null;
    }

    public List<RequestEntity> totalFundsAllocatedForSphere(long sphereId){
        return null;
    }



    public float percentageOfSuccessfullyCompletedProjects(Field field){
        return 0;
    }

    public long sumForConsiderRequest(long sphereId){
        return 0;
    }

    @Override
    public RequestEntity getRequestByProject(ProjectEntity projectEntity) {
        Criteria cr = getSession().createCriteria(RequestEntity.class, "Request")
                .add(Restrictions.eq("project",projectEntity));
        return (RequestEntity) cr.uniqueResult();
    }

    private void getMoneyCount(PocketEntity pocketEntity)
    {

    }


    }

